//
//  teacherViewController.m
//  ioschishan
//
//  Created by 千翼网络科技 on 2017/7/15.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "teacherViewController.h"
#import "cityListViewController.h"
#import "schoolChangeViewController.h"

@interface teacherViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong) UIScrollView *mianScrollView;

@property(nonatomic,strong) UIScrollView *bgScrollView;

@property(nonatomic,strong) UITextField *nameTextField;
@property(nonatomic,strong) UITextField *numTextField;
@property(nonatomic,strong) UITextField *schoolTextField;
@property(nonatomic,strong) NSString *schoolId;

@property(nonatomic,strong) NSMutableArray *dataArr;

@end

@implementation teacherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self photo];
}

#pragma mark ------------------ init -------------------

- (void)photo{
    
    _mianScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, screenScale(216), scrWidth(), scrWidth()/3*4)];
    _mianScrollView.contentSize = CGSizeMake(scrWidth(), scrWidth()/3*4);
    _mianScrollView.bounces = YES;
    _mianScrollView.showsVerticalScrollIndicator = NO;
    _mianScrollView.backgroundColor = [UIColor whiteColor];
    [_bgScrollView addSubview:_mianScrollView];
    [_mianScrollView setDelegate:self];
    self.showInView = _mianScrollView;
    self.maxCount = 10;
    [self initPickerView];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(scrWidth()/3, scrWidth()/3*3 - screenScale(10), scrWidth()/3, scrWidth()/3 )];
    view.backgroundColor = [UIColor whiteColor];
    [_mianScrollView addSubview:view];
}

- (void)initView{
    
    self.title = @"认证教师";
    self.view.backgroundColor = getColor(@"f5f5f5");
    
    ///
    
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, scrWidth(), scrHeight() - 64 - screenScale(49))];
    _bgScrollView.contentSize = CGSizeMake(scrWidth(), scrHeight() - 64 - screenScale(49) + scrWidth()/3*1.5);
    _bgScrollView.bounces = YES;
    _bgScrollView.showsVerticalScrollIndicator = YES;
    _bgScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_bgScrollView];
    [_bgScrollView setDelegate:self];
    
    ///
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(schoolNotification:) name:@"school" object:nil];
    
    UILabel *topLabel = [ConfigXp labelWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(44))
                                            text:@"    验证信息"
                                       textColor:getColor(@"333333")
                                            font:get4sFont(13)
                                   textAlignment:NSTextAlignmentLeft
                                 backgroundColor:getColor(@"f5f5f5")];
    [_bgScrollView addSubview:topLabel];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(44), scrWidth(), screenScale(300))];
    bgView.backgroundColor = getColor(@"ffffff");
    [_bgScrollView addSubview:bgView];
    
    ///
    
    UILabel *oldPwdLabel = [ConfigXp labelWithFrame:CGRectMake(0, screenScale(44), scrWidth(), screenScale(44))
                                               text:@"    真实姓名"
                                          textColor:getColor(@"4a4a4a")
                                               font:get4sFont(13)
                                      textAlignment:NSTextAlignmentLeft
                                    backgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:oldPwdLabel];
    
    _nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(screenScale(87), screenScale(44 + 15), scrWidth() - screenScale( 87 + 15), screenScale(15))];
    _nameTextField.placeholder = @"填写真实姓名";
    _nameTextField.textColor = getColor(@"4a4a4a");
    _nameTextField.font = get4sFont(12);
    _nameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_bgScrollView addSubview:_nameTextField];
    
    UIView *oldThreadView = [[UIView alloc]initWithFrame:CGRectMake(screenScale(15), screenScale(44+44), scrWidth() - screenScale(15), screenScale(0.5))];
    oldThreadView.backgroundColor = getColor(@"eeeeee");
    [_bgScrollView addSubview:oldThreadView];
    
    ///
    
    UILabel *newPwdLabel = [ConfigXp labelWithFrame:CGRectMake(0, screenScale(44*2), scrWidth(), screenScale(44))
                                               text:@"    身份证号"
                                          textColor:getColor(@"4a4a4a")
                                               font:get4sFont(13)
                                      textAlignment:NSTextAlignmentLeft
                                    backgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:newPwdLabel];
    
    _numTextField = [[UITextField alloc]initWithFrame:CGRectMake(screenScale(87), screenScale(44*2 + 15), scrWidth() - screenScale( 87 + 15), screenScale(15))];
    _numTextField.placeholder = @"填写身份证号";
    _numTextField.textColor = getColor(@"4a4a4a");
    _numTextField.font = get4sFont(12);
    _numTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_bgScrollView addSubview:_numTextField];
    
    UIView *threadView = [[UIView alloc]initWithFrame:CGRectMake(screenScale(15), screenScale(128), scrWidth() - screenScale(15), screenScale(0.5))];
    threadView.backgroundColor = getColor(@"eeeeee");
    [_bgScrollView addSubview:threadView];
    
    ///
    
    UILabel *schoolLabel = [ConfigXp labelWithFrame:CGRectMake(0, screenScale(44*3-2), scrWidth(), screenScale(44))
                                               text:@"    学校名称"
                                          textColor:getColor(@"4a4a4a")
                                               font:get4sFont(13)
                                      textAlignment:NSTextAlignmentLeft
                                    backgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:schoolLabel];
    
    _schoolTextField = [[UITextField alloc]initWithFrame:CGRectMake(screenScale(87), screenScale(44*3 + 15-2), scrWidth() - screenScale( 87 + 15), screenScale(15))];
    _schoolTextField.enabled = NO;
    _schoolTextField.placeholder = @"选择你所在的学校";
    _schoolTextField.textColor = getColor(@"4a4a4a");
    _schoolTextField.font = get4sFont(12);
    _schoolTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_bgScrollView addSubview:_schoolTextField];
    
    UIView *threadView1 = [[UIView alloc]initWithFrame:CGRectMake(screenScale(15), screenScale(172), scrWidth() - screenScale(15), screenScale(0.5))];
    threadView1.backgroundColor = getColor(@"eeeeee");
    [_bgScrollView addSubview:threadView1];
    
    UIImageView *rightImg = [[UIImageView alloc]initWithFrame:CGRectMake(scrWidth() - screenScale(16), screenScale(142), screenScale(12), screenScale(12))];
    rightImg.image = [UIImage imageNamed:@"arrow_icon"];
    [_bgScrollView addSubview:rightImg];
    
    UIButton *schoolBtn = [ConfigXp buttonWithFrame:CGRectMake(0, screenScale(128), scrWidth(), screenScale(44))
                                              title:nil
                                         titleColor:nil
                                               font:nil
                                    backgroundColor:nil
                                              image:nil];
    [schoolBtn addTarget:self action:@selector(schoolBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_bgScrollView addSubview:schoolBtn];
                                                               
    ///
    
    UILabel *proveLabel = [ConfigXp labelWithFrame:CGRectMake(0, screenScale(44*4), scrWidth(), screenScale(44))
                                               text:@"    资质证明"
                                          textColor:getColor(@"4a4a4a")
                                               font:get4sFont(13)
                                      textAlignment:NSTextAlignmentLeft
                                    backgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:proveLabel];
    
    ///
    
    UIButton *finishBtn = [ConfigXp buttonWithFrame:CGRectMake(screenScale(30), screenScale(370), screenScale(315), screenScale(44))
                                              title:@"确定"
                                         titleColor:getColor(@"ffffff")
                                               font:get4sFont(13)
                                    backgroundColor:getColor(@"3a95ff")
                                              image:nil];
    finishBtn.layer.cornerRadius = screenScale(3);
    [finishBtn addTarget:self action:@selector(finishBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:finishBtn];
    [finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).with.offset(0);
        make.left.mas_equalTo(self.view).with.offset(0);
        make.right.mas_equalTo(self.view).with.offset(0);
        make.height.mas_equalTo(screenScale(49));
    }];
}

#pragma mark ------------------ NSNotification -------------------

- (void)schoolNotification:(NSNotification *)text{
    
    if ([text.userInfo[@"school"]isEqualToString:@"yes"]) {
        
        _schoolTextField.text = text.userInfo[@"what"];
        _schoolId = text.userInfo[@"schoolId"];
    }
}

#pragma mark ------------------ click -------------------

- (void)finishBtnClick{
    
//    [self.navigationController popViewControllerAnimated:YES];
    
    if (_nameTextField.text.length < 1) {
        
        [PublicFunction showAlert:nil message:@"请填写真实姓名" duration:1.0f];
        return;
        
    }
    
    if (_schoolTextField.text.length < 1) {
        
        [PublicFunction showAlert:nil message:@"请选择学校" duration:1.0f];
        return;
        
    }
    
    if (![TSRegularExpressionUtil validateIdentityCard:_numTextField.text]) {
        
        [PublicFunction showAlert:nil message:@"请填写正确的身份证号" duration:1.0];
        return;
    }
    
    if (self.getBigImageArray.count < 1) {
        
        NSLog(@"bigImageArray===%@",self.getBigImageArray);
        [PublicFunction showAlert:nil message:@"请选择学校照片" duration:1.0];
        return;
        
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    [productParams setValue: _numTextField.text forKey:@"id_card"];
    [productParams setValue: _nameTextField.text forKey:@"full_name"];
    [productParams setValue: _schoolTextField.text forKey:@"school_name"];
    [productParams setValue: _schoolId forKey:@"school_id"];
    
    NSLog(@"productParams===\n\n%@",productParams);
    
    //两种方式上传头像
    /*方式一：使用NSData数据流传图片*/
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/plain",
                                                         @"text/javascript",
                                                         @"text/json",
                                                         @"text/html",
                                                         @"image/jpeg", nil];
    [manager POST:more_editData parameters:productParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        __block NSInteger i = 0;
        [self.bigImageArray enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            //压缩图片
            i++;
            // NSData *imageData = UIImagePNGRepresentation(obj);
            NSData *imageData = UIImageJPEGRepresentation(obj,0.005);
            
            
            //压缩图片
            
            //拼接图片名称+时间确保不重复命名
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *imageFileName = [NSString stringWithFormat:@"%@%ld.jpg", [formatter stringFromDate:[NSDate date]],i];
            // 测试输出文件名
            
            //上传图片，以文件流的格式
            
            [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"image%ld",i] fileName:imageFileName mimeType:@"image/JPG"];
            
            
        }];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSString *strtmp = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSString *requestTmp = [NSString stringWithString: strtmp];
        NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];  //解析
        
        //        [self.navigationController popToRootViewControllerAnimated:YES];
        
        
        
        NSLog(@"+++>%@", resultDic);
        
        NSLog(@"--->%@", resultDic[@"msg"]);
        
        [hud removeFromSuperview];
        
        //        phoneCodeViewController *vc = [[phoneCodeViewController alloc]init];
        //        [self.navigationController pushViewController:vc animated:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"--->%@", error);
        
        [hud removeFromSuperview];
        
    }];
}

- (void)schoolBtnClick{

//    schoolChangeViewController *vc = [[schoolChangeViewController alloc]init];
//    [self.navigationController pushViewController:vc animated:YES];
    
    cityListViewController *vc = [[cityListViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)imgBtnClick{

    
}

@end
